<?php 
define('APPLOGVIEW','1.00');
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

//_admin application

loadlib("libArchivedir");

?>
    <?php if (admin_isadmin()) {
        admin_writelog("admin: logview");
	$op=$_GET['op'];

    ?>
	<p>
	<?php

	    $type=$_POST['type'];
	    $year=$_POST['year'];
	    $month=$_POST['month'];
	    $day=$_POST['day'];
	    $noview=0;
	    if ($type==""){
		$type=$_GET['type'];
		$year=$_GET['year'];
		$month=$_GET['month'];
		$day=$_GET['day'];
		if ($type==""){
		    $type="global";
		    $noview=1;
		}
	    }
	    else{
		if ($year!=date("Y") || $month!=date("m") || $day!=date("d"))
		    if (!file_exists("datas/sb/".sb_get()."/logs/global/$year/$month/$day/log"))
			complog($year,$month,$day);
	    }


	    if ($year=="")$year=date("Y");
	    if ($month=="")$month=date("m");
	    if ($day=="")$day=date("d");




	    $key=$_GET['key'];
	    if ($key!="") $noview=0;
	?>
	<div align='center'>

	<?php
	    // zip mensile-------------------------
	    if ($op=='log-mzip'){

	    $year=$_POST['year'];
	    $month=$_POST['month'];

	    echo "zip mensile $month - $year <br />";
	    $nome_zip="datas/sb/".sb_get()."/logs/$year-$month.zip";
	    $zip = new zip_file($nome_zip);
	    $zip->set_options(array('inmemory'=>"0",'overwrite'=>1,'storepaths','prepend','level','type'=>"zip"));

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/global/$year/$month");
	    $dir->load();
	    foreach($dir->files as $nf){
		$zip->add_files($nf);
	    }
	    echo "logs global...done <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/admin/$year/$month");
	    $dir->load();
	    foreach($dir->files as $nf){
		$zip->add_files($nf);
	    }
	    echo "logs admin...done <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/errors/$year/$month");
	    $dir->load();
	    foreach($dir->files as $nf){
		$zip->add_files($nf);
	    }
	    echo "logs errors...done <br />";

	    $zip->create_archive();

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/global/$year/$month");
	    $dir->load();
	    foreach($dir->files as $nf){
		unlink($nf);
	    }
	    echo "logs global...deleted <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/admin/$year/$month");
	    $dir->load();
	    foreach($dir->files as $nf){
		unlink ($nf);
	    }
	    echo "logs admin...deleted <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/errors/$year/$month");
	    $dir->load();
	    foreach($dir->files as $nf){
		unlink ($nf);
	    }
	    echo "logs errors...deleted <br />";

	    echo "scarica <a href='$nome_zip'>logs.zip</a>\n";


	}
	else{ ?>

	    <table style="font-size:9;" border='0'>
	    <tr>
	    <td>
	    <fieldset >
	    <form name='ziplog' action='index.php?appname=<?=$appname;?>&op=log-mzip'  method='POST'>
		    <b>zip mensile dei logs </b> year <select name='year' >
		    <option><?=$year?></option>
		    <?php
			$path="datas/sb/".sb_get()."/logs/global";
			$fd=opendir($path);
			$i=0;
			while (false !== ($nf= readdir($fd))){
			    if ($nf[0]!='.'){
				echo"<option>$nf</option>";
				if ( $nf == $year){
				    $def_year=$i;
				}
				$i++;
			    }
			}
			closedir($fd);
		    ?>
		 </select>

		month<select name='month'>
		    <option><?=$month?></option>
		    <?php
			for($i=1;$i<=12 ;$i++)
			    echo"<option>".substr("0".$i,-2)."</option>";
		    ?>
		 </select>
		<input type='submit' value='zip' />
	    </form>
	    </fieldset>

	<?php } ?>
	    </td><td>


	<?php
	// zip annuale -------------------------

	if ($op=='log-yzip'){
	    $year=$_POST['year'];

	    echo "zip annuale $year <br />";
	    $nome_zip="datas/sb/".sb_get()."/logs/$year.zip";
	    $zip = new zip_file($nome_zip);
	    $zip->set_options(array('inmemory'=>"0",'overwrite'=>1,'storepaths','prepend','level','type'=>"zip"));

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/global/$year");
	    $dir->load();
	    foreach($dir->files as $nf){
		$zip->add_files($nf);
	    }
	    echo "logs global...done <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/admin/$year");
	    $dir->load();
	    foreach($dir->files as $nf){
		$zip->add_files($nf);
	    }
	    echo "logs admin...done <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/errors/$year");
	    $dir->load();
	    foreach($dir->files as $nf){
		$zip->add_files($nf);
	    }
	    echo "logs errors...done <br />";

	    $zip->create_archive();

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/global/$year");
	    $dir->load();
	    foreach($dir->files as $nf){
		unlink ($nf);
	    }
	    echo "logs global...deleted <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/admin/$year");
	    $dir->load();
	    foreach($dir->files as $nf){
		unlink ($nf);
	    }
	    echo "logs admin...deleted <br />";

	    $dir = new DeepDir();
	    $dir->setDir("datas/sb/".sb_get()."/logs/errors/$year");
	    $dir->load();
	    foreach($dir->files as $nf){
		unlink ($nf);
	    }
	    echo "logs errors...deleted <br />";



	    echo "scarica <a href='$nome_zip'>logs.zip</a>\n";

	}
	else{ ?>
	    <fieldset>
	    <form name='ziplog' action='index.php?appname=<?=$appname;?>&op=log-yzip'  method='POST'>
		    <b>zip annuale dei logs </b> year <select name='year' >
		    <option><?=$year?></option>
		    <?php
			$path="datas/sb/".sb_get()."/logs/global";
			$fd=opendir($path);
			$i=0;
			while (false !== ($nf= readdir($fd))){
			    if ($nf[0]!='.'){
				echo"<option>$nf</option>";
				if ( $nf == $year){
				    $def_year=$i;
				}
				$i++;
			    }
			}
			closedir($fd);
		    ?>
		 </select>
		<input type='submit' value='zip' />
	    </form>
	    </fieldset>
	<?php } ?>
	    </td>
	    </tr>
	    </table>

	<fieldset>
	<br/>
	    <form name='sellog' action='index.php?appname=<?=$appname;?>&op=log-view'  method='POST'>
		<b> visualizza </b> type <select name='type'>
		    <option><?=$type?></option>
		    <?php
			$path="datas/sb/".sb_get()."/logs";
			$fd=opendir($path);
			$i=0;
			while (false !== ($nf= readdir($fd))){
			    if ($nf[0] !='.' && is_dir("datas/sb/".sb_get()."/logs/$nf")!=false){
				echo"<option>$nf</option>";
				if ( $nf == $type){
				    $def_type=$i;
				}
				$i++;
			    }
			}
			closedir($fd);
		    ?>

		</select>

		year <select name='year' >
		    <?php
			if (is_dir("datas/sb/".sb_get()."/logs/$type")){
			    echo"<option>$year</option>";
			    $path="datas/sb/".sb_get()."/logs/$type";
			    $fd=opendir($path);
			    $i=0;
			    while (false !== ($nf= readdir($fd))){
				if ($nf[0] !='.' && is_dir("datas/sb/".sb_get()."/logs/$type/$nf")!=false){
				    echo"<option>$nf</option>";
				    if ( $nf == $year){
					$def_year=$i;
				    }
				    $i++;
				}
			    }
			    closedir($fd);
			}
		    ?>
		 </select>

		month<select name='month'>
		    <?php
			for($i=1;$i<=12 ;$i++)
			    echo"<option>".substr("0".$i,-2)."</option>";
		    ?>
		 </select>

		day<select name='day'>
		    <?php
			for($i=1;$i<=31 ;$i++)
			    echo"<option>".substr("0".$i,-2)."</option>";
		    ?>
		</select>

		<script>document.sellog.type.selectedIndex='<?=$def_type+1; ?>';</script>
		<script>document.sellog.year.selectedIndex='<?=$def_year; ?>';</script>
		<script>document.sellog.month.selectedIndex='<?=$month-1; ?>';</script>
		<script>document.sellog.day.selectedIndex='<?=$day-1; ?>';</script>

		<input type='submit' value='vai' />
	    </form>
	</fieldset>
    </div>

<?php
	if ($noview==0){

	    if (file_exists("datas/sb/".sb_get()."/logs/global/$year/$month/$day")){
		$fd=opendir("datas/sb/".sb_get()."/logs/global/$year/$month/$day/");
		while (false !== ($nf= readdir($fd))){
		    if($nf[0]!='.'){
			if (file_exists("datas/sb/".sb_get()."/logs/global/$year/$month/$day/$nf"))
			    include "datas/sb/".sb_get()."/logs/global/$year/$month/$day/$nf";
		    }
		}
		closedir($fd);
	    }

	    if (file_exists("datas/sb/".sb_get()."/logs/admin/$year/$month/$day")){
		$fd=opendir("datas/sb/".sb_get()."/logs/admin/$year/$month/$day/");
		while (false !== ($nf= readdir($fd))){
		    if($nf[0]!='.'){
			if (file_exists("datas/sb/".sb_get()."/logs/admin/$year/$month/$day/$nf"))
			    include "datas/sb/".sb_get()."/logs/admin/$year/$month/$day/$nf";
		    }
		}
		closedir($fd);
	    }

	    if (file_exists("datas/sb/".sb_get()."/logs/errors/$year/$month/$day")){
		$fd=opendir("datas/sb/".sb_get()."/logs/errors/$year/$month/$day/");
		while (false !== ($nf= readdir($fd))){
		    if($nf[0]!='.'){
			if (file_exists("datas/sb/".sb_get()."/logs/errors/$year/$month/$day/$nf"))
			    include "datas/sb/".sb_get()."/logs/errors/$year/$month/$day/$nf";
		    }
		}
		closedir($fd);
	    }

	    if ($key != ""  && $_GET['type']==""){
		echo "<br/><font color='red'>Global</font><br/>";
		echo "<table id='view-log' title='global' class='class_table' width='96%'>";
		echo "<tr><td class='firstrow'>DATE TIME</td><td>$key</td></tr>";
		echo "<tr><td class='firstrow'>URI</td><td>".str_replace("="," =",$URI[$key])."</td></tr>";
		echo "<tr><td class='firstrow'>ADDR</td><td>".$REMOTE_ADDR[$key]."</td></tr>";
		echo "<tr><td class='firstrow'>HOST</td><td>".gethostbyaddr($REMOTE_ADDR[$key])."</td></tr>";
		echo "<tr><td class='firstrow'>USER</td><td>".$USER[$key]."</td></tr>";
		echo "</table>";

		echo "<br/><font color='red'>Admin</font><br/>";
		echo "<table id='view-log' class='class_table' width='96%'>";
		echo "<tr><td class='firstrow'>DATE TIME</td><td>$key</td></tr>";
		echo "<tr><td class='firstrow'>ADMIN OP</td><td>".$ADMINOP[$key]."</td></tr>";
		echo "</table>";

		echo "<br/><font color='red'>Errors</font><br/>";
		if (count($erno)!=0){
		    foreach( $erno as $akey => $aerno){
			if (substr_count($akey,$key)!=0){
			    echo "<table id='view-log' class='class_table' width='96%'>";
			    echo "<tr><td class='firstrow'>DATE TIME</td><td>$akey</td></tr>";
			    echo "<tr><td class='firstrow'>ERRSTR</td><td>".$errstr[$akey]."</td></tr>";
			    echo "<tr><td class='firstrow'>ERNO</td><td>".$erno[$akey]."</td></tr>";
			    echo "<tr><td class='firstrow'>ERRFILE</td><td>".$errfile[$akey]."</td></tr>";
			    echo "<tr><td class='firstrow'>ERRLINE</td><td>".$errline[$akey]."</td></tr>";
			    echo "</table>";
			    echo "<br/>";
			}
		    }
		}
	    }

	    switch($type){

	    case "global":
		if ($URI)krsort($URI);
		if ($REMOTE_ADDR)krsort($REMOTE_ADDR);
		if ($REMOTE_HOST)krsort($REMOTE_HOST);
		if ($USER)krsort($USER);
		if ($key==""){
		    echo"<table class='class_table'>";
		    echo"<tr class='firstrow'>
		    <td rowspan='2' width='80'>ORA</td>
		    <td>ADDR</td>
		    <td>USER</td>
		    </tr>
		    <tr class='firstrow'>
		    <td colspan='3'>URI</td>
		    </tr>
		    </table>";
		    if(count($URI)!=0){
			foreach($URI as $k =>$uri ){
			    echo"<br/><table id='view-log' class='class_table'>";
			    echo "<tr>\n";
			    $key="<a href='index.php?appname=$appname&op=log-view&key=$k&year=$year&month=$month&day=$day'>$k</a>";
			    echo "<td rowspan='2' width='80'>".$key."</td>";
			    $url_key="<a href='index.php?appname=$appname&op=log-view&type=url&year=$year&month=$month&day=$day&url-key=".$REMOTE_ADDR[$k]."'>".$REMOTE_ADDR[$k]."</a>";
			    echo "<td>".$url_key."</td>";
			    $user_key="<a href='index.php?appname=$appname&op=log-view&type=user&year=$year&month=$month&day=$day&user-key=".$USER[$k]."'>".substr($USER[$k],0,-34)."</a>";
			    echo "<td>".$user_key."</td>";
			    echo "</tr>";
			    echo "<tr>";
			    echo "<td colspan='3'>".str_replace("="," =",str_replace("/","/ ",$URI[$k]))."</td>";
			    echo "</tr>";
			    echo "</table>\n";
			}
		    }
		}
		break;

	    case "admin":
		if ($ADMINOP)krsort($ADMINOP);
		if ($key==""){
		    echo"<table id='view-log' class='class_table' >";
		    echo"<tr class='firstrow'>
		    <td width='80'>ORA</td>
		    <td>ADMINOP</td>
		    </tr>
		    ";
		    if(count($ADMINOP)!=0){
			foreach($ADMINOP as $k =>$adminop ){
			    $key="<a href='index.php?appname=$appname&op=log-view&key=$k&year=$year&month=$month&day=$day'>$k</a>";
			    echo "
			    <tr>
			    <td width='80'>".$key."</td>
			    <td>".str_replace(":"," :",$ADMINOP[$k])."</td>
			    </tr>
			    ";
			}
		    }
		    echo"</table>";
		}
		break;

	    case "errors":
		if ($erno)krsort($erno);
		if ($errstr)krsort($errstr);
		if ($errfile)krsort($errfile);
		if ($errline)krsort($errline);
		if ($key==""){
		    echo"<table id='view-log' class='class_table' >";
		    echo"<tr class='firstrow'>
		    <td rowspan='2' width='100'>ORA</td>
		    <td width='60'>ERNO</td>
		    <td width='60'>ERRLINE</td>
		    <td>ERRFILE</td>
		    </tr>
		    <tr class='firstrow'>
		    <td colspan='3'>ERRSTR</td>
		    </tr>
		    </table>
		    ";
		    if(count($erno)!=0){
			foreach($erno as $k =>$_erno ){
			    echo"<br/><table id='view-log' class='class_table' >";
			    $key="<a href='index.php?appname=$appname&op=log-view&key=".substr($k,0,-5)."&year=$year&month=$month&day=$day'>$k</a>";
			    $errfile[$k]=str_replace("/","/ ",$errfile[$k]);
			    $errfile[$k]=str_replace("\\","\\ ",$errfile[$k]);
			    echo "
			    <tr>
			    <td width='100'>".$key."</td>
			    <td width='60'>".$erno[$k]."</td>
			    <td width='60'>".$errline[$k]."</td>
			    <td>".$errfile[$k]."</td>
			    </tr>
			    <tr>
			    <td></td>
			    <td colspan='3'>".$errstr[$k]."</td>
			    </tr>
			    </table>

			    ";
			}
		    }
		}
		break;

	    case "url":

		if ($URI)krsort($URI);
		if ($REMOTE_ADDR)krsort($REMOTE_ADDR);
		if ($REMOTE_HOST)krsort($REMOTE_HOST);
		if ($USER)krsort($USER);

		echo"<table class='class_table' >";
		echo"<tr class='firstrow'>
		<td rowspan='2' width='80'>ORA</td>
		<td>ADDR</td>
		<td>USER</td>
		</tr>
		<tr class='firstrow'>
		<td colspan='3'>URI</td>
		</tr>
		</table>
		";
		foreach($URI as $k =>$uri ){
		    if ($REMOTE_ADDR[$k]==$_GET['url-key']){
			echo"<br/><table class='class_table' >";
			echo "<tr>\n";
			$key="<a href='index.php?appname=$appname&op=log-view&key=$k&year=$year&month=$month&day=$day'>$k</a>";
			echo "<td rowspan='2' width='80'>".$key."</td>";
			$url=$REMOTE_ADDR[$k]."</a>";
			echo "<td>".$url."</td>";
			$user_key="<a href='index.php?appname=$appname&op=log-view&type=user&year=$year&month=$month&day=$day&user-key=".$USER[$k]."'>".substr($USER[$k],0,-34)."</a>";
			echo "<td>".$user_key."</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td colspan='3'>".str_replace("="," =",str_replace("/","/ ",$URI[$k]))."</td>";
			echo "</tr>";
			echo"</table>";
		    }
		}
		break;

	    case "user":

		if ($URI)krsort($URI);
		if ($REMOTE_ADDR)krsort($REMOTE_ADDR);
		if ($REMOTE_HOST)krsort($REMOTE_HOST);
		if ($USER)krsort($USER);

		echo"<table class='class_table'>";
		echo"<tr class='firstrow'>
		<td rowspan='2' width='80'>ORA</td>
		<td>ADDR</td>
		<td>USER</td>
		</tr>
		<tr class='firstrow'>
		<td colspan='3'>URI</td>
		</tr>
		</table>
		";
		foreach($URI as $k =>$uri ){
		    if ($USER[$k]==$_GET['user-key']){
			echo"<br/><table class='class_table'>";
			echo "<tr>\n";
			$key="<a href='index.php?appname=$appname&op=log-view&key=$k&year=$year&month=$month&day=$day'>$k</a>";
			echo "<td rowspan='2' width='80'>".$key."</td>";
			$url_key="<a href='index.php?appname=$appname&op=log-view&type=url&year=$year&month=$month&day=$day&url-key=".$REMOTE_ADDR[$k]."'>".$REMOTE_ADDR[$k]."</a>";
			echo "<td>".$url_key."</td>";
			//echo "<td>".gethostbyaddr($REMOTE_ADDR[$k])."</td>";
			echo "<td>".substr($USER[$k],0,-34)."</td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td colspan='3'>".str_replace("="," =",str_replace("/","/ ",$URI[$k]))."</td>";
			echo "</tr>";
			echo"</table>";
		    }
		}
		break;

	    }
	}
	echo "</p>";
    }else{ ?>
	<p>administration privilege requested </p>
    <?php }?>
